/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import Entity.*;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;
import org.jbox2d.callbacks.ContactImpulse;
import org.jbox2d.callbacks.ContactListener;
import org.jbox2d.collision.Manifold;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.Fixture;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;
import org.jbox2d.dynamics.contacts.Contact;

/**
 *
 * @author Maravillas
 */
public class ToCreateWorld {

    private World world;
    private ArrayList<Body> planetBulletCollided;
    private ArrayList<Body> spaceShipBulletCollided;
    private ArrayList<Body> enemyBulletCollided;
    private ArrayList<Body> enemyBombCollided;
    private int delay;
    private int numberEnemy;
    private int health = 6;
    private int healthEnemy = 4;
    private boolean created = true;

    //World
    public World createWorld() {
        world = new World(new Vec2(0, 0));
        planetBulletCollided = new ArrayList<>();
        spaceShipBulletCollided = new ArrayList<>();
        enemyBulletCollided = new ArrayList<>();
        enemyBombCollided = new ArrayList<>();
        delay = 0;
        numberEnemy = 1;
        return world;
    }

    //Creation space ship
    public SpaceShip createSpaceShip(int WIGTH, int HEIGHT) {
        SpaceShip spaceShip = new SpaceShip();
        BodyDef bd = spaceShip.createBodyDef(WIGTH / 2, HEIGHT / 2, BodyType.DYNAMIC, 300);
        FixtureDef fd = spaceShip.createBodyFixture(1, 1, 1);
        spaceShip.createBody(bd, fd, world, "spaceShip");
        return spaceShip;
    }

    //Creation of starts
    public ArrayList createStarts(int WIDTH, int HEIGHT) {
        ArrayList<Vec2> starts = new ArrayList<>();
        Random random = new Random(0);
        for (int i = 0; i < 5000; i++) {
            Vec2 start = new Vec2(random.nextInt(WIDTH * 10), random.nextInt(HEIGHT * 10));
            starts.add(start);
        }
        return starts;
    }

    //Creation of objets (bodies)
    public ArrayList createPlanets(int WIDTH, int HEIGHT) {
        ArrayList<Planet> planets = new ArrayList<>();
        Random random = new Random(0);
        Color color;
        for (int i = 0; i < 200; i++) {
            color = Color.getHSBColor(random.nextFloat(), random.nextFloat(), random.nextFloat());
            Planet planet = new Planet();
            BodyDef bd = planet.createBodyDef(random.nextInt(WIDTH * 10), random.nextInt(HEIGHT * 10), BodyType.DYNAMIC, random.nextInt(1000));
            FixtureDef fd = planet.createBodyFixture(1, 1, 1);
            planet.createBody(bd, fd, world, "planet");
            planet.setColor(color);
            planets.add(planet);

        }
        for (int i = 0; i < 200; i++) {
            color = Color.getHSBColor(random.nextFloat(), random.nextFloat(), random.nextFloat());
            Planet planet = new Planet();
            BodyDef bd = planet.createBodyDef(-random.nextInt(WIDTH * 10), -random.nextInt(HEIGHT * 10), BodyType.DYNAMIC, random.nextInt(1000));
            FixtureDef fd = planet.createBodyFixture(1, 1, 1);
            planet.createBody(bd, fd, world, "planet");
            planet.setColor(color);
            planets.add(planet);

        }
        for (int i = 0; i < 200; i++) {
            color = Color.getHSBColor(random.nextFloat(), random.nextFloat(), random.nextFloat());
            Planet planet = new Planet();
            BodyDef bd = planet.createBodyDef(random.nextInt(WIDTH * 10), -random.nextInt(HEIGHT * 10), BodyType.DYNAMIC, random.nextInt(1000));
            FixtureDef fd = planet.createBodyFixture(1, 1, 1);
            planet.createBody(bd, fd, world, "planet");
            planet.setColor(color);
            planets.add(planet);

        }
        for (int i = 0; i < 200; i++) {
            color = Color.getHSBColor(random.nextFloat(), random.nextFloat(), random.nextFloat());
            Planet planet = new Planet();
            BodyDef bd = planet.createBodyDef(-random.nextInt(WIDTH * 10), random.nextInt(HEIGHT * 10), BodyType.DYNAMIC, random.nextInt(1000));
            FixtureDef fd = planet.createBodyFixture(1, 1, 1);
            planet.createBody(bd, fd, world, "planet");
            planet.setColor(color);
            planets.add(planet);

        }
        return planets;
    }

    public Bullet createBullet(int x, int y, Vec2 direction) {
        Bullet bullet = new Bullet();
        BodyDef bd = bullet.createBodyDef(x, y, BodyType.DYNAMIC, 80);
        FixtureDef fd = bullet.createBodyFixture(1, 1, 1);
        bullet.createBody(bd, fd, world, "bullet");
        bullet.setDirection(direction);
        return bullet;
    }

    public Bomb createBomb(int x, int y, Vec2 direction) {
        Bomb bomb = new Bomb();
        BodyDef bd = bomb.createBodyDef(x, y, BodyType.DYNAMIC, 100);
        FixtureDef fd = bomb.createBodyFixture(1, 1, 1);
        bomb.createBody(bd, fd, world, "bomb");
        bomb.setDirection(direction);
        return bomb;
    }

    public Bullet createBulletEnemy(int x, int y, Vec2 direction) {
        Bullet bullet = new Bullet();
        BodyDef bd = bullet.createBodyDef(x, y, BodyType.DYNAMIC, 60);
        FixtureDef fd = bullet.createBodyFixture(1, 1, 1);
        bullet.createBody(bd, fd, world, "bulletEnemy");
        bullet.setDirection(direction);
        return bullet;
    }

    public EnemyBagel createEnemyBagel(int x, int y) {
        EnemyBagel enemyBagel = new EnemyBagel();
        BodyDef bd = enemyBagel.createBodyDef(x - 100, y - 200, BodyType.DYNAMIC, 100);
        FixtureDef fd = enemyBagel.createBodyFixture(1, 1, 1);
        enemyBagel.createBody(bd, fd, world, "enemy");
        return enemyBagel;
    }

    public EnemyElongatedSpiral createEnemyElogatedSpiral(int x, int y) {
        EnemyElongatedSpiral enemyElongatedSpiral = new EnemyElongatedSpiral();
        BodyDef bd = enemyElongatedSpiral.createBodyDef(x - 100, y - 200, BodyType.DYNAMIC, 100);
        FixtureDef fd = enemyElongatedSpiral.createBodyFixture(1, 1, 1);
        enemyElongatedSpiral.createBody(bd, fd, world, "enemy");
        return enemyElongatedSpiral;
    }

    public EnemyLadyBug createEnemyLadyBug(int x, int y) {
        EnemyLadyBug enemyLadyBug = new EnemyLadyBug();
        BodyDef bd = enemyLadyBug.createBodyDef(x - 100, y - 200, BodyType.DYNAMIC, 100);
        FixtureDef fd = enemyLadyBug.createBodyFixture(1, 1, 1);
        enemyLadyBug.createBody(bd, fd, world, "enemy");
        return enemyLadyBug;
    }

    public EnemyRectangle createEnemyRetangle(int x, int y) {
        EnemyRectangle enemyRectangle = new EnemyRectangle();
        BodyDef bd = enemyRectangle.createBodyDef(x - 100, y - 200, BodyType.DYNAMIC, 100);
        FixtureDef fd = enemyRectangle.createBodyFixture(1, 1, 1);
        enemyRectangle.createBody(bd, fd, world, "enemy");
        return enemyRectangle;
    }

    public EnemySpiral createEnemySpiral(int x, int y) {
        EnemySpiral enemyEllipse = new EnemySpiral();
        BodyDef bd = enemyEllipse.createBodyDef(x - 100, y - 200, BodyType.DYNAMIC, 50);
        FixtureDef fd = enemyEllipse.createBodyFixture(1, 1, 1);
        enemyEllipse.createBody(bd, fd, world, "enemy");
        return enemyEllipse;
    }

    //Listener
    public ContactListener createContactListener() {
        ContactListener contactListener = new ContactListener() {
            Fixture fA;
            Fixture fB;

            @Override
            public void beginContact(Contact contact) {
                fA = contact.getFixtureA();
                fB = contact.getFixtureB();
                if (fA.getBody() != null && fB.getBody() != null) {
                    if (fA.getBody().getUserData().toString().equals("planet") && fB.getBody().getUserData().toString().equals("bullet")) {
                        planetBulletCollided.add(fA.getBody());
                        planetBulletCollided.add(fB.getBody());
                    }

                    if (fA.getBody().getUserData().toString().equals("spaceShip") && fB.getBody().getUserData().toString().equals("bulletEnemy")) {
                        spaceShipBulletCollided.add(fA.getBody());
                        spaceShipBulletCollided.add(fB.getBody());
                        setHealth(health - 1);
                    }

                    if (fA.getBody().getUserData().toString().equals("enemy") && fB.getBody().getUserData().toString().equals("bullet")) {
                        enemyBulletCollided.add(fA.getBody());
                        enemyBulletCollided.add(fB.getBody());
                        setHealthEnemy(healthEnemy - 1);
                    }

                    if (fA.getBody().getUserData().toString().equals("enemy") && fB.getBody().getUserData().toString().equals("bomb")) {
                        enemyBombCollided.add(fA.getBody());
                        enemyBombCollided.add(fB.getBody());
                        setHealthEnemy(healthEnemy - 2);
                    }
                }
            }

            @Override
            public void endContact(Contact contact) {
                //
            }

            @Override
            public void preSolve(Contact contact, Manifold oldManifold) {
                //System.out.println("Contact presolve");
            }

            @Override
            public void postSolve(Contact contact, ContactImpulse impulse) {
                //System.out.println("Contact postsolve");
            }
        };
        return contactListener;
    }

//GETTER AND SETTER
    public ArrayList<Body> getPlanetBulletCollided() {
        return planetBulletCollided;
    }

    public void setPlanetBulletCollided(int index) {
        this.planetBulletCollided.remove(index);
    }

    public ArrayList<Body> getSpaceShipBulletCollided() {
        return spaceShipBulletCollided;
    }

    public void setSpaceShipBulletCollided(int index) {
        this.spaceShipBulletCollided.remove(index);
    }

    public ArrayList<Body> getEnemyBulletCollided() {
        return enemyBulletCollided;
    }

    public void setEnemyBulletCollided(int index) {
        this.enemyBulletCollided.remove(index);
    }

    public ArrayList<Body> getEnemyBombCollided() {
        return enemyBombCollided;
    }

    public void setEnemyBombCollided(int index) {
        this.enemyBombCollided.remove(index);
    }

    public int getDelay() {
        return delay;
    }

    public void setDelay(int delay) {
        this.delay = delay;
    }

    public int getNumberEnemy() {
        return numberEnemy;
    }

    public void setNumberEnemy(int numberEnemy) {
        this.numberEnemy = numberEnemy;
    }

    public int getHealth() {
        return health;
    }

    public void setHealth(int health) {
        this.health = health;
    }

    public int getHealthEnemy() {
        return healthEnemy;
    }

    public void setHealthEnemy(int healthEnemy) {
        this.healthEnemy = healthEnemy;
    }

    public boolean isCreated() {
        return created;
    }

    public void setCreated(boolean created) {
        this.created = created;
    }

}
